package top.forforever.srb.core.controller.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.forforever.srb.common.exception.Assert;
import top.forforever.srb.common.result.R;
import top.forforever.srb.common.result.ResponseEnum;
import top.forforever.srb.core.pojo.entity.IntegralGrade;
import top.forforever.srb.core.service.IntegralGradeService;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Mr.Jiang
 * @since 2023-04-29
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级功能接口")
@Slf4j
public class AdminIntegralGradeController {

    @Autowired
    IntegralGradeService integralGradeService;

    @GetMapping("list")
    @ApiOperation(value = "获取所有积分等级集合")
    public R getList() {
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list",list);
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "根据id获取积分等级信息")
    public R getById(@ApiParam(value = "积分等级id") @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        Assert.notNull(integralGrade,ResponseEnum.ERROR);
        return R.ok().data("integralGrade",integralGrade);
    }

    @PostMapping("save")
    public R save(@RequestBody IntegralGrade integralGrade) {
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
        Integer integralStart = integralGrade.getIntegralStart();
        Integer integralEnd = integralGrade.getIntegralEnd();
        Assert.isTrue(borrowAmount.longValue() > 0 && integralStart > 0 && integralEnd > 0 && integralStart < integralEnd, ResponseEnum.BORROW_INTEGRAL_NUM_ERROR);
        boolean isSave = integralGradeService.save(integralGrade);
        Assert.isTrue(isSave,ResponseEnum.ERROR);
        return R.ok();
    }

    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable Long id) {
        boolean isDelete = integralGradeService.removeById(id);
        Assert.isTrue(isDelete,ResponseEnum.ERROR);
        return R.ok();
    }

    @PutMapping("updateById")
    public R updateById(@RequestBody IntegralGrade integralGrade) {
        boolean isUpdate = integralGradeService.updateById(integralGrade);
        Assert.isNull(isUpdate,ResponseEnum.ERROR);
        return R.ok();
    }
}

